using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;

namespace XProject.Util
{
    class EventXMLIE
    {
        public int Export(IList<Entity.Event> event_list,string output_file)
        {
            XmlTextWriter wr = new XmlTextWriter(output_file, Encoding.UTF8);
            int count = 0;
            wr.WriteStartDocument(true);
            //root
            wr.WriteStartElement("EventList");
            foreach (Entity.Event ev in event_list)
            {

              //start event
                wr.WriteStartElement("Event");
                //start time
                wr.WriteStartElement("StartTime");
                wr.WriteValue(ev.StartTime);
                wr.WriteEndElement();
                //end time
                wr.WriteStartElement("EndTime");
                wr.WriteValue(ev.EndTime);
                wr.WriteEndElement();
                //Lunar day
                wr.WriteStartElement("LunarDay");
                wr.WriteValue(ev.LunarDay);
                wr.WriteEndElement();
                //lunar month
                wr.WriteStartElement("LunarMonth");
                wr.WriteValue(ev.LunarMonth);
                wr.WriteEndElement();
                //content
                wr.WriteStartElement("Content");
                wr.WriteValue(ev.Content);
                wr.WriteEndElement();
                //place
                wr.WriteStartElement("Place");
                wr.WriteValue(ev.Place);
                wr.WriteEndElement();
                //type id
                wr.WriteStartElement("TypeID");
                wr.WriteValue(ev.Type.TypeID);
                wr.WriteEndElement();
                //priority
                wr.WriteStartElement("Priority");
                wr.WriteValue(ev.Priority);
                wr.WriteEndElement();
                //note
                wr.WriteStartElement("Note");
                wr.WriteValue(ev.Note);
                wr.WriteEndElement();
                //is public
                wr.WriteStartElement("IsPublic");
                wr.WriteValue(ev.IsPublic);
                wr.WriteEndElement();
                //end event
                wr.WriteEndElement();
                count++;
            }
            //end root
            wr.WriteEndElement();
            wr.WriteEndDocument();
            wr.Close();
            return count;
        }
        public IList<Entity.Event> Import(string input_file)
        {
            IList<Entity.Event> list = new List<Entity.Event>();
            XmlTextReader rd = new XmlTextReader(input_file);
            Entity.Event ev = new XProject.Entity.Event();
            try
            {   
          string node_name = "";
                while (rd.Read())
                {
                    
                    if (rd.NodeType == XmlNodeType.Element)
                    {
                        node_name = rd.Name.ToLower();
                        if(node_name=="event")ev = new XProject.Entity.Event();
                    }
                    else if (rd.NodeType == XmlNodeType.Text)
                        {                            
                            switch (node_name)
                            {                                
                                case "starttime": ev.StartTime = rd.ReadContentAsDateTime(); break;
                                case "endtime": ev.EndTime = rd.ReadContentAsDateTime(); break;
                                case "lunarday": ev.LunarDay = (byte)rd.ReadContentAsInt(); break;
                                case "lunarmonth": ev.LunarMonth = (byte)rd.ReadContentAsInt(); break;
                                case "content": ev.Content = rd.ReadContentAsString(); break;
                                case "place": if (rd.Value != "") ev.Place = rd.ReadContentAsString(); break;
                                case "typeid": ev.Type.TypeID = rd.ReadContentAsInt(); break;
                                case "priority": ev.Priority = (byte)rd.ReadContentAsInt(); break;
                                case "note": if (rd.Value != "") ev.Note = rd.ReadContentAsString(); break;
                                case "ispublish": ev.IsPublic = rd.ReadElementContentAsBoolean(); break;
                            }
                        }

                        else if (rd.NodeType == XmlNodeType.EndElement)
                        {
                            if (rd.Name.ToLower() == "event") list.Add(ev);
                        }
                }
            }
            catch (XmlException ex) { }
            catch (Exception ex) { }
            return list;
        }
    }
}
